<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updateUser">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api'

export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetailByIdFn()
  },
  methods: {
    // 根据id获取员工信息
    async getUserDetailByIdFn() {
      try {
        // console.log(this.$route)
        const res = await getUserDetailByIdAPI(this.$route.query.id)
        // console.log(res)
        // 数据回填
        this.userInfo.username = res.data.username
        this.userInfo.password = res.data.password
        this.userInfo.mobile = res.data.mobile
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 更新员工信息
    updateUser() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        // 接口要求的数据中必须有id,而this.userInfo中没有,所以要加一条id
        const res = await saveUserDetailByIdAPI({
          ...this.userInfo,
          id: this.$route.query.id
        })
        console.log(res)
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
